﻿/// <reference path="jquery-1.2.6.min.js" />

$(document).ready(function() {
    //gets the chat history box.
    var objDiv = document.getElementById('divChatHistory');
    objDiv.scrollTop = objDiv.scrollHeight;

    var senderName = document.getElementById('hfRecipient').value;
    var recipientName = document.getElementById('hfSender').value;
    //sends a request to get messages from the database.
    chatProg.getMessages(senderName, recipientName, '0', OnWSRequestComplete);

    $.timer(2000, function(timer) {
        //get messages every 2 seconds.
        getMessages(senderName, recipientName);
    });
});
///<summary>
/// sends the message to be inserted into the databse.
///</summary>
///<param name="message" optional="false">this is the actual message.</param>
function SetMessages(message) {
    var senderName = document.getElementById('hfRecipient').value;
    var recipientName = document.getElementById('hfSender').value;
    chatProg.setMessage(message, recipientName, senderName); //sends the messages.
};
///<summary>
/// gets the messages from the database.
///</summary>
///<param name="sender" optional="false">the sender user name</param>
///<param name="recipient" optional="false">the recipient user name</param>
function getMessages(sender, recipient) {
    var lastUid = document.getElementById('tbChatID').value;
    chatProg.getMessages(sender, recipient, lastUid, OnWSRequestComplete);
};
///<summary>
/// returns the messages from the database
///</summary>
///<param name="results" optional="false">the messages in a string[] value</param>
function OnWSRequestComplete(results) {
    var objDiv = document.getElementById('divChatHistory');
    //sets the message number.
    if (results[1] != null) {
        document.getElementById('tbChatID').value = results[1];
        }
    //updates the messages in the message box.
    if (results[0] != null) { 
        objDiv.innerHTML += results[0];
    }
    objDiv.scrollTop = objDiv.scrollHeight;
};

